<template>
  <div class="wrapper">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{path:'/5-1'}">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{path:'/5-1'}">模块管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-row>
      <el-col :span="3" :offset="20">
        添加模块
        <el-button circle type="primary" icon="el-icon-circle-plus" @click="newadd">
        </el-button>
      </el-col>
      <el-col :span="20" :offset="2">
        <el-table :data="tableData">
          <el-table-column type="index" label="序号"></el-table-column>
          <el-table-column prop="modelName" label="模块名称"></el-table-column>
          <el-table-column label="URL地址" prop="modelUrl"></el-table-column>
          <el-table-column label="创建人" prop="createBy">
            <template slot-scope="scope">
              <el-tag :type="scope.row.createBy | statusFilter">{{scope.row.createBy | formatStata}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column>
            <template slot-scope="scope">
              <el-button @click="del(scope.row.id)" type="danger">删除</el-button>
              <el-button @click="toedit(scope.row.id)" type="success">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import axios from 'axios'
  import {
    modulelist, moduleson,delmodule
  } from '@/api/navbar/navbar'
  export default {
    name: "ModuleManagement",
    inject: ['reload'],
    data() {
      return {
        tableData: [],
        fatherid:[],
        childid:[],
        child:[]
      }
    },
    methods: {
      newadd() {
        let obj=JSON.stringify(this.fatherid);
        let chi=JSON.stringify(this.child);
        console.log(obj);
        this.$router.push({name: 'ModuleAdd',query:{fatherid:obj,childid:chi}})
      },
      getNavbarInfo() {
        let params={};
        params.page=1;
        params.size=10;
        modulelist(params).then(this.getNavbarInfoSucc).then(this.getsonid)
      },
      getNavbarInfoSucc(res) {
           console.log(res);
          this.tableData = res.data.UserList;
          for(let item of this.tableData){
            this.fatherid.push({
              value:item.id,
              label:item.modelName
            })
          }
          return this.fatherid
      },
      getsonid(res){
        let _this=this;
        moduleson().then(function (res) {
         for(let item of res.data.UserList){
           _this.childid.push(item.id)
         }
         for(let item of res.data.UserList){
           _this.child.push({
             value:item.id,
             label:item.modelName
           })
         }
        });
        console.log(this.child);
      },
      toedit (id) {
        let obj=JSON.stringify(this.child);
        this.$router.push({name:'ModuleEdit',query:{index:id,childid:obj}})
      },
      del(id) {
        console.log(id);
        this.$confirm('确定删除模块信息吗?',
          '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
            center: true
          }).then(() => {
          this.$message({
            type: 'success',
            message: '已删除模块'
          });
          let _this = this;
          delmodule(id).then(function (res) {
            console.log(res)
            _this.reload();
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
    },
    filters: {
      // el-tag类型转换
      statusFilter(status) {
        const statusMap = {
          0: 'success',
          1: 'primary',
          39:'danger'
        };
        return statusMap[status]
      },
      // 状态显示转换
      formatStata(status) {
        const statusMap = {
          0: 'admin',
          1: 'superadmin',
          39:'ultimateadmin'
        };
        return statusMap[status]
      },
    },
    mounted() {
      this.getNavbarInfo();
    }
  }
</script>

<style lang="less" scoped>
  @import "~styles/z.less";
  @import "~styles/css3.less";
  @import "~styles/variables.less";
  .wrapper{
    .w100;
  }
</style>
